This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: some packages are treated as externals when they shouldn't (#771)
* fix: some packages are treated as externals when they shouldn't In case `externals` are passed to webpack.config.js, they are converted to regular expression. The idea is to detect all imports for this module, for example if it is `nativescript-vue`, we would like to make externals imports like `nativescript-vue/somesubdir/file1`. However, the regular expression we construct also matches packages like `nativescript-vue-template`. Fix the regular expressions in all template webpack.config.js files and add unit tests for them. Add devDependency to `proxyquire` as the webpack configs that are tested, require some packages that are not available in node_modules of `nativescript-dev-webpack` (including the `nativescript-dev-webpack` itself). This is expected for the `webpack.config.js` files, as they should be placed in the NativeScript applications, so mocking the requires shouldn't be a problem in this case. * chore: move externals generation to a common place Move the conversion of regular expressions from `string[]` to `RegExp[]` to index.js file, so it can be reused in all webpack.config.js files. Move most of the tests to the `index.spec.js` due to this change. * chore: fix grammar errors Co-Authored-By: rosen-vladimirov <rosen-vladimirov@users.noreply.github.com>
- Loading branch information
1 parent
c7bcbaf
commit 3362cd6
Showing
8 changed files
with
194 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { getConvertedExternals } from './index'; | ||
|
||
describe('index', () => { | ||
describe('getConvertedExternals', () => { | ||
it('returns empty array when nullable is passed', () => { | ||
const actualResult = getConvertedExternals(null); | ||
expect(actualResult).toEqual([]); | ||
}); | ||
|
||
const testCases = [ | ||
{ | ||
input: ['nativescript-vue'], | ||
expectedOutput: [/^nativescript-vue((\/.*)|$)/] | ||
}, | ||
{ | ||
input: ['nativescript-vue', 'nativescript-angular'], | ||
expectedOutput: [/^nativescript-vue((\/.*)|$)/, /^nativescript-angular((\/.*)|$)/] | ||
} | ||
]; | ||
|
||
testCases.forEach(testCase => { | ||
it('converts passed strings to regular expressions', () => { | ||
const actualResult = getConvertedExternals(testCase.input); | ||
expect(actualResult).toEqual(testCase.expectedOutput); | ||
}); | ||
|
||
it(`returns regular expressions which match expected modules and their subdirs, for input ${testCase.input}`, () => { | ||
[ | ||
'nativescript-vue', | ||
'nativescript-vue/subdir', | ||
'nativescript-vue/subdir/subdir-2' | ||
].forEach(testString => { | ||
const regExpsExternals = getConvertedExternals(testCase.input); | ||
const result = regExpsExternals.some((regExp: RegExp) => !!regExp.exec(testString)); | ||
expect(result).toBe(true, `String ${testString} does not match any of the regular expressions: ${regExpsExternals.join(', ')}`); | ||
}); | ||
}); | ||
|
||
it(`returns regular expressions which does not match expected modules and their subdirs, for input ${testCase.input}`, () => { | ||
[ | ||
'nativescript-facebook', | ||
'nativescript-facebook/nativescript-vue', | ||
'main-plugin/subdir/nativescript-vue', | ||
'nativescript-vue-template-compiler', | ||
'nativescript-vue-template-compiler/subdir' | ||
].forEach(testString => { | ||
const regExpsExternals = getConvertedExternals(testCase.input); | ||
const result = regExpsExternals.some((regExp: RegExp) => !!regExp.exec(testString)); | ||
expect(result).toBe(false, `String ${testString} matches some of the regular expressions: ${regExpsExternals.join(', ')}`); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as proxyquire from 'proxyquire'; | ||
import * as nsWebpackIndex from '../index'; | ||
// With noCallThru enabled, `proxyquire` will not fall back to requiring the real module to populate properties that are not mocked. | ||
// This allows us to mock packages that are not available in node_modules. | ||
// In case you want to enable fallback for a specific object, just add `'@noCallThru': false`. | ||
proxyquire.noCallThru(); | ||
|
||
class EmptyClass { }; | ||
|
||
const nativeScriptDevWebpack = { | ||
GenerateBundleStarterPlugin: EmptyClass, | ||
WatchStateLoggerPlugin: EmptyClass, | ||
PlatformFSPlugin: EmptyClass, | ||
getAppPath: () => 'app', | ||
getEntryModule: () => 'EntryModule', | ||
getResolver: () => null, | ||
getConvertedExternals: nsWebpackIndex.getConvertedExternals | ||
}; | ||
|
||
const emptyObject = {}; | ||
|
||
const webpackConfigAngular = proxyquire('./webpack.angular', { | ||
'nativescript-dev-webpack': nativeScriptDevWebpack, | ||
'nativescript-dev-webpack/nativescript-target': emptyObject, | ||
'nativescript-dev-webpack/transformers/ns-replace-bootstrap': emptyObject, | ||
'nativescript-dev-webpack/transformers/ns-replace-lazy-loader': emptyObject, | ||
'nativescript-dev-webpack/utils/ast-utils': emptyObject, | ||
'@ngtools/webpack': { | ||
AngularCompilerPlugin: EmptyClass | ||
} | ||
}); | ||
|
||
const webpackConfigTypeScript = proxyquire('./webpack.typescript', { | ||
'nativescript-dev-webpack': nativeScriptDevWebpack, | ||
'nativescript-dev-webpack/nativescript-target': emptyObject, | ||
}); | ||
|
||
const webpackConfigJavaScript = proxyquire('./webpack.javascript', { | ||
'nativescript-dev-webpack': nativeScriptDevWebpack, | ||
'nativescript-dev-webpack/nativescript-target': emptyObject, | ||
}); | ||
|
||
const webpackConfigVue = proxyquire('./webpack.vue', { | ||
'nativescript-dev-webpack': nativeScriptDevWebpack, | ||
'nativescript-dev-webpack/nativescript-target': emptyObject, | ||
'vue-loader/lib/plugin': EmptyClass, | ||
'nativescript-vue-template-compiler': emptyObject | ||
}); | ||
|
||
describe('webpack.config.js', () => { | ||
[ | ||
{ type: 'javascript', webpackConfig: webpackConfigJavaScript }, | ||
{ type: 'typescript', webpackConfig: webpackConfigTypeScript }, | ||
{ type: 'angular', webpackConfig: webpackConfigAngular }, | ||
{ type: 'vue', webpackConfig: webpackConfigVue } | ||
].forEach(element => { | ||
const { type, webpackConfig } = element; | ||
|
||
describe(`verify externals for webpack.${type}.js`, () => { | ||
const getInput = (platform: string, externals: string[]) => { | ||
const input: any = { externals }; | ||
input[platform] = true; | ||
return input; | ||
}; | ||
|
||
[ | ||
'android', | ||
'ios' | ||
].forEach(platform => { | ||
describe(`for ${platform}`, () => { | ||
afterEach(() => { | ||
nativeScriptDevWebpack.getConvertedExternals = nsWebpackIndex.getConvertedExternals; | ||
}); | ||
|
||
it('returns empty array when externals are not passed', () => { | ||
const config = webpackConfig(getInput(platform, null)); | ||
expect(config.externals).toEqual([]); | ||
}); | ||
|
||
it('calls getConvertedExternals to convert externals', () => { | ||
let isCalled = false; | ||
nativeScriptDevWebpack.getConvertedExternals = () => { | ||
isCalled = true; | ||
return []; | ||
}; | ||
|
||
const input = getInput(platform, ['nativescript-vue']); | ||
webpackConfig(input); | ||
expect(isCalled).toBe(true, 'Webpack.config.js must use the getConvertedExternals method'); | ||
}); | ||
|
||
[ | ||
{ | ||
input: ['nativescript-vue'], | ||
expectedOutput: [/^nativescript-vue((\/.*)|$)/] | ||
}, | ||
{ | ||
input: ['nativescript-vue', 'nativescript-angular'], | ||
expectedOutput: [/^nativescript-vue((\/.*)|$)/, /^nativescript-angular((\/.*)|$)/] | ||
}, | ||
].forEach(testCase => { | ||
const input = getInput(platform, testCase.input); | ||
|
||
it(`are correct regular expressions, for input ${testCase.input}`, () => { | ||
const config = webpackConfig(input); | ||
expect(config.externals).toEqual(testCase.expectedOutput); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters