Skip to content

Commit

Permalink
feat: taro plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cncolder committed Aug 2, 2020
1 parent 49382fa commit 63b6694
Show file tree
Hide file tree
Showing 6 changed files with 735 additions and 11 deletions.
8 changes: 7 additions & 1 deletion config/index.ts
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs'
import webpack from 'webpack'
import webpackChain from 'webpack-chain'
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import { IProjectConfig } from '@tarojs/taro/types/compile'
import { TaroProvidePlugin } from '@tarojsx/polyfill/dist/plugins'

Expand All @@ -20,9 +21,11 @@ const config: IProjectConfig = {
alias: {
'@': path.resolve(__dirname, '..', 'src'),
},
plugins: [path.resolve(__dirname, '../plugin.js')],
mini: {
webpackChain(chain: webpackChain, webpack: webpack.Configuration) {
chain.plugin('taroProviderPlugin').use(TaroProvidePlugin, [['default', 'Intl']])
// chain.plugin('taroProviderPlugin').use(TaroProvidePlugin, [['default', 'Intl']])
// chain.plugin('optimizeCssAssetsPlugin').use(OptimizeCssAssetsPlugin, [{ assetNameRegExp: /\.wxss$/g }])
},
postcss: {
autoprefixer: {
Expand All @@ -47,6 +50,9 @@ const config: IProjectConfig = {
},
},
},
miniCssExtractPluginOption: {
ignoreOrder: true,
},
},
h5: {
publicPath: '/',
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
"babel-plugin-import": "^1.13.0",
"babel-preset-taro": "^3.0.0-rc.4",
"node-sass": "^4.14.1",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"typescript": "^3.9.5",
"webpack-chain": "^6.4.0"
},
Expand Down
31 changes: 31 additions & 0 deletions plugin.js
@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = (function (ctx, options) {
var prod = process.env.NODE_ENV === 'production';
ctx.modifyWebpackChain(function (_a) {
var chain = _a.chain;
// 支持 node 风格的 __filename 全局变量
if ((options === null || options === void 0 ? void 0 : options.node) !== false) {
chain.node.set('__filename', true);
}
// 允许 import .d.ts 文件, 内容最终会被 babel-typescript 清空.
if ((options === null || options === void 0 ? void 0 : options.resolve) !== false && !chain.resolve.extensions.has('.d.ts')) {
chain.resolve.extensions.add('.d.ts');
}
// if (options?.tarojsxProvidePlugin !== false && !chain.plugins.has('taroProvidePlugin')) {
// try {
// chain
// .plugin('taroProvidePlugin')
// .use(require('@tarojsx/polyfill/dist/plugins/TaroProvidePlugin').default)
// } catch (err) {}
// }
// // production 环境下优化去重 wxss
// if (prod && options?.optimizeCssAssetsPlugin !== false && chain.plugins.has('optimizeCssAssetsPlugin')) {
// try {
// chain
// .plugin('optimizeCssAssetsPlugin')
// .use(require('optimize-css-assets-webpack-plugin'), [{ assetNameRegExp: /\.wxss$/g }])
// } catch (err) {}
// }
});
});
42 changes: 42 additions & 0 deletions plugin.ts
@@ -0,0 +1,42 @@
import webpackChain from 'webpack-chain'
import { IPluginContext } from '@tarojs/service'

export interface TarojsxPluginOptions {
node?: boolean
resolve?: boolean
tarojsxProvidePlugin?: boolean
optimizeCssAssetsPlugin?: boolean
}

export default (ctx: IPluginContext, options: TarojsxPluginOptions) => {
const prod = process.env.NODE_ENV === 'production'

ctx.modifyWebpackChain(({ chain }: { chain: webpackChain }) => {
// 支持 node 风格的 __filename 全局变量
if (options?.node !== false) {
chain.node.set('__filename', true)
}

// 允许 import .d.ts 文件, 内容最终会被 babel-typescript 清空.
if (options?.resolve !== false && !chain.resolve.extensions.has('.d.ts')) {
chain.resolve.extensions.add('.d.ts')
}

// if (options?.tarojsxProvidePlugin !== false && !chain.plugins.has('taroProvidePlugin')) {
// try {
// chain
// .plugin('taroProvidePlugin')
// .use(require('@tarojsx/polyfill/dist/plugins/TaroProvidePlugin').default)
// } catch (err) {}
// }

// // production 环境下优化去重 wxss
// if (prod && options?.optimizeCssAssetsPlugin !== false && chain.plugins.has('optimizeCssAssetsPlugin')) {
// try {
// chain
// .plugin('optimizeCssAssetsPlugin')
// .use(require('optimize-css-assets-webpack-plugin'), [{ assetNameRegExp: /\.wxss$/g }])
// } catch (err) {}
// }
})
}
4 changes: 3 additions & 1 deletion tsconfig.json
Expand Up @@ -5,9 +5,11 @@
"removeComments": false,
"preserveConstEnums": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"jsx": "preserve",
Expand Down

0 comments on commit 63b6694

Please sign in to comment.