Programing

node_modules를 제외하지 않는 Webpack

lottogame 2020. 10. 26. 07:39
반응형

node_modules를 제외하지 않는 Webpack


나는 내가 구축하고있는 Node 프레임 워크에 webpack을 사용하고 있습니다. (물론 gulp를 사용해야 할 것입니다.) 내가 EJS 모듈을 포함하면, webpack은 node_modules dir을 제외하도록 명시 적으로 지시하더라도 컴파일 된 소스에 포함합니다.

module.exports = {
    context: __dirname,
    target: 'node',
    // ...
    output: {
        libraryTarget: 'commonjs'
        // ...
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader?{ "stage": 0, "optional": ["runtime"] }'
            }
        ]
    }
};

보시다시피 JS 파일에 대한 테스트가 있으며 node_modules를 제외하도록 지시합니다. 내 제외를 무시하는 이유는 무엇입니까?


만 제외하고 같은 설정 파일에서, 그것은 것 node_modules으로 해석되는 babel-loader, 하지만 번들로 제공되는.

node_modules번들링에서 기본 노드 라이브러리 를 제외 하려면 다음을 수행해야합니다.

  1. 에 추가 target: 'node'하십시오 webpack.config.js. 이렇게하면 기본 노드 모듈 (경로, fs 등)이 번들에서 제외 됩니다.
  2. 다른 .NET 을 제외 하려면 webpack-node-externals사용 하세요node_modules .

따라서 결과 구성 파일은 다음과 같아야합니다.

var nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    target: 'node', // in order to ignore built-in modules like path, fs, etc. 
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder 
    ...
};

타이프 라이터를 사용할 때이 문제에 실행하는 경우, 당신은 추가해야 할 수 있습니다 skipLibCheck: true귀하의 tsconfig.json파일.


절대 경로를 사용해보십시오.

exclude:path.resolve(__dirname, "node_modules")

WebPack 1.x :

내 자신을 지정 resolve.extensions했지만 빈 확장을 포함하지 못했기 때문에 이것은 나에게 발생 했습니다 ''.

resolve: {
  extensions: ['.js', '.jsx']
},

(더 이상 사용할 수 없습니다!) 웹팩 문서에서 :

이 옵션을 설정하면 기본값이 재정의됩니다. 즉, 웹팩이 더 이상 기본 확장을 사용하여 모듈을 해석하지 않습니다. 확장명이 필요한 모듈 (예 : require ( './ somefile.ext'))을 올바르게 해석하려면 배열에 빈 문자열을 포함해야합니다. 마찬가지로 확장자없이 필요한 모듈 (예 : require ( 'underscore'))을 ".js"확장자를 가진 파일로 해석하려면 배열에 ".js"를 포함해야합니다.

빈 확장을 추가하면 오류가 해결되었습니다.

resolve: {
  extensions: ['', '.js', '.jsx']
},

이것은 나를 위해 일했습니다.

제외 : [/ bower_components /, / node_modules /]

module.loaders

자동으로 적용된 로더의 배열입니다.

각 항목은 다음 속성을 가질 수 있습니다.

테스트 : 충족해야하는 조건

제외 : 충족되지 않아야하는 조건

포함 : 충족해야하는 조건

loader: A string of "!" separated loaders

loaders: A array of loaders as string

A condition can be a RegExp, an absolute path start, or an array of one of these combined with "and".

See http://webpack.github.io/docs/configuration.html#module-loaders

참고URL : https://stackoverflow.com/questions/33001237/webpack-not-excluding-node-modules

반응형