Skip to content

Commit

Permalink
ADD types && fix a matching bug & add tests && use destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Jun 11, 2019
1 parent 68b469d commit b0eaf55
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
6 changes: 6 additions & 0 deletions dist-ts/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NormalModuleReplacementPlugin } from 'webpack';
export declare const rewriteCoreJsRequest: (originalRequest: string) => string;
export interface Options {
resolveFrom: string | false;
}
export default function CoreJSUpgradeWebpackPlugin(options: Options): NormalModuleReplacementPlugin;
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
"description": "a webpack plugin that transforms core-js v2 paths to core-js v3 paths",
"keywords": [
"core-js",
"webpack",
"transform",
"path",
"resolve"
"resolve",
"transform",
"webpack"
],
"license": "MIT",
"author": "Norbert de Langen <[email protected]>",
"main": "dist/index.js",
"scripts": {
"prepublish": "npm run build",
"build": "babel src -d dist --extensions '.ts'",
"generate-types": "tsc --emitDeclarationOnly --declaration src/index.ts --esModuleInterop --outDir dist",
"prepublish": "npm run build && npm run generate-types",
"test": "jest"
},
"dependencies": {
Expand All @@ -28,6 +29,7 @@
"@babel/preset-typescript": "^7.3.3",
"@types/webpack": "^4.4.32",
"eslint": "^5.16.0",
"jest": "^24.8.0"
"jest": "^24.8.0",
"typescript": "^3.5.1"
}
}
23 changes: 23 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import CoreJsAutoUpgradePlugin, { rewriteCoreJsRequest } from './index';

describe('CoreJsAutoUpgradePlugin', () => {
it('should be constructable, without options', () => {
expect(() => new CoreJsAutoUpgradePlugin()).not.toThrow()
});
it('should be constructable, with options', () => {
expect(() => new CoreJsAutoUpgradePlugin({
resolveFrom: __dirname,
})).not.toThrow()
});
});

describe('rewriteCoreJsRequest', () => {
const fakeRequire = jest.mock();

Expand All @@ -15,6 +26,18 @@ describe('rewriteCoreJsRequest', () => {
'core-js/modules/esnext.math.iaddh.js'
);
});

it('should rewrite `core-js/modules/es.*` import to `core-js/modules/es.*`', () => {
expect(rewriteCoreJsRequest('core-js/modules/es.object.is-frozen.js')).toBe(
'core-js/modules/es.object.is-frozen.js'
);
});

it('should rewrite `core-js/modules/esnext.*` import to `core-js/modules/esnext.*`', () => {
expect(rewriteCoreJsRequest('core-js/modules/esnext.set.some.js')).toBe(
'core-js/modules/esnext.set.some.js'
);
});
});

describe('rewrite `core-js/library/fn/*`', () => {
Expand Down
16 changes: 5 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const rewriteAndPreservePrefix = (originalRequest: string, newPath: string, newM
};

export const rewriteCoreJsRequest = (originalRequest: string) => {
if (/core-js\/modules\/(.*)/.test(originalRequest)) {
const modulesInformations = originalRequest.match(/core-js\/modules\/es(6|7)\.(.*)/);
const esVersion = modulesInformations[1];
const originalPath = modulesInformations[2];
if (/core-js\/modules\/es(6|7)\.(.*)/.test(originalRequest)) {
const [,esVersion, originalPath] = originalRequest.match(/core-js\/modules\/es(6|7)\.(.*)/);

if (esVersion === '6') {
return rewriteAndPreservePrefix(originalRequest, `modules/es.${originalPath}`);
Expand All @@ -23,16 +21,13 @@ export const rewriteCoreJsRequest = (originalRequest: string) => {
}

if (/core-js\/library\/fn\/(.*)/.test(originalRequest)) {
const modulesInformations = originalRequest.match(/core-js\/library\/fn\/(.*)/);
const originalPath = modulesInformations[1];
const [,originalPath] = originalRequest.match(/core-js\/library\/fn\/(.*)/);

return rewriteAndPreservePrefix(originalRequest, `features/${originalPath}`, 'core-js-pure');
}

if (/core-js\/es(5|6|7)(.*)/.test(originalRequest)) {
const modulesInformations = originalRequest.match(/core-js\/es(5|6|7)(.*)?/);
const esVersion = modulesInformations[1];
const originalPath = modulesInformations[2];
const [,esVersion, originalPath] = originalRequest.match(/core-js\/es(5|6|7)(.*)?/);

if (esVersion === '5') {
return null;
Expand All @@ -48,8 +43,7 @@ export const rewriteCoreJsRequest = (originalRequest: string) => {
}

if (/core-js\/(object)\/(.*)/.test(originalRequest)) {
const modulesInformations = originalRequest.match(/core-js\/(.*)?/);
const originalPath = modulesInformations[1];
const [,originalPath] = originalRequest.match(/core-js\/(.*)?/);

return rewriteAndPreservePrefix(originalRequest, `features/${originalPath}`);
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5428,6 +5428,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.1.tgz#ba72a6a600b2158139c5dd8850f700e231464202"
integrity sha512-64HkdiRv1yYZsSe4xC1WVgamNigVYjlssIoaH2HcZF0+ijsk5YK2g0G34w9wJkze8+5ow4STd22AynfO6ZYYLw==

uglify-js@^3.1.4:
version "3.6.0"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
Expand Down

0 comments on commit b0eaf55

Please sign in to comment.